﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace pwestcott.Util.Tests.Essence.CompareTo
{
    [TestFixture]
    public class CompareToFieldObjectTests
    {
        [EssenceClass(UseIn = EssenceFunctions.All)]
        class TestEssence : IComparable<TestEssence>
        {
            [Essence]
            public int MyInt;

            public int CompareTo(TestEssence other)
            {
                return Essence<TestEssence>.CompareToStatic(this, other);
            }
        }

        class TestManual : IComparable<TestManual>
        {
            public int MyInt;

            public int CompareTo(TestManual other)
            {
                if (other == null)
                    return -1;
                if (ReferenceEquals(this, other))
                    return 0;

                int result;

                result = MyInt.CompareTo(other.MyInt);
                if (result != 0)
                    return result;

                return 0;
            }
        }

        [Test]
        public void NullNullTest()
        {
            var e1 = new TestEssence();
            var e2 = new TestEssence();
            var m1 = new TestManual();
            var m2 = new TestManual();

            Assert.AreEqual(0, m1.CompareTo(m2));
            Assert.AreEqual(m1.CompareTo(m2), e1.CompareTo(e2));
        }

        [Test]
        public void ValueNullTest()
        {
            var e1 = new TestEssence();
            var e2 = new TestEssence();
            var m1 = new TestManual();
            var m2 = new TestManual();

            e1.MyInt = m1.MyInt = 42;

            Assert.AreEqual(1, m1.CompareTo(m2));
            Assert.AreEqual(m1.CompareTo(m2), e1.CompareTo(e2));
        }

        [Test]
        public void NullValueTest()
        {
            var e1 = new TestEssence();
            var e2 = new TestEssence();
            var m1 = new TestManual();
            var m2 = new TestManual();

            e2.MyInt = m2.MyInt = 42;

            Assert.AreEqual(-1, m1.CompareTo(m2));
            Assert.AreEqual(m1.CompareTo(m2), e1.CompareTo(e2));
        }

        [Test]
        public void ValueValueTest()
        {
            var e1 = new TestEssence();
            var e2 = new TestEssence();
            var m1 = new TestManual();
            var m2 = new TestManual();

            e1.MyInt = e2.MyInt = m1.MyInt = m2.MyInt = 42;

            Assert.AreEqual(0, m1.CompareTo(m2));
            Assert.AreEqual(m1.CompareTo(m2), e1.CompareTo(e2));
        }
    }
}
